#include "loginframe.h"
#include "ui_loginframe.h"


LoginFrame::LoginFrame(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginFrame)
{
    ui->setupUi(this);
    connect(ui->pushButton_ll,SIGNAL(clicked()),this,SLOT(LoginFunction()));
    connect(ui->pushButton_lc,SIGNAL(clicked()),this,SLOT(DealLoginAnswer()));
}

LoginFrame::~LoginFrame()
{
    delete ui;
}

void LoginFrame::setPtr(int *p,QString * str)
{
    ptr = p;
    name = str;
}


void LoginFrame::LoginFunction()
{
    Login login;
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();
    sprintf(login.username,username.toStdString().c_str());
    sprintf(login.password,password.toStdString().c_str());

    login.cmd = CMD_LOGIN;
    //QMessageBox::information(this,"inf",login.username);
    //QMessageBox::information(this,"inf",login.password);

    this->username = username;
    int res = socket->write((const char *)&login,MSG_LENGTH);
    if(res < 0)
    {
        QMessageBox::critical(this,"error","错误");
    };
    connect(socket,SIGNAL(readyRead()),this,SLOT(DealLoginAnswer()));
}

void LoginFrame::DealLoginAnswer()
{
    QByteArray array = socket->readAll();
    char * ptrc = array.data();
    Answer * answer = (Answer *)ptrc;
    *name = username;
    if(answer->cmd == CMD_REG_USERINFO_ANSWER)
    {
        if(answer->answer == true)
        {
            QMessageBox::information(this,"通知","登录成功");
            *ptr = 1;
            //qDebug()<<(*ptr)<<"  "<<ptr;
            this->close();
        }else
        {
            *ptr = 0;
            QMessageBox::information(this,"通知","登录失败");
        }
    }
    else{
    if(answer->answer == true)
    {
        QMessageBox::information(this,"通知","注册成功");
        info = new UserInfo;
        info->setInfo(socket,this->username);
        info->show();
        info->exec();

        //this->close();
    }
    else
        QMessageBox::information(this,"通知","注册失败");
    }

}


